package pojo.handlers;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.handlers.HandlerUtil;

import pojo.wizards.DatabaseWizardPage;
import pojo.wizards.JavaBeanWizard;

import org.eclipse.jface.dialogs.IPageChangingListener;
import org.eclipse.jface.dialogs.PageChangingEvent;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.jface.wizard.WizardPage;

public class SampleHandler extends AbstractHandler {

	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
		JavaBeanWizard wizard = new JavaBeanWizard();
		WizardDialog dialog = new WizardDialog(window.getShell(), wizard);
		dialog.addPageChangingListener(new IPageChangingListener() {
			@Override
			public void handlePageChanging(PageChangingEvent event) {
				WizardPage currentPage = (WizardPage) event.getCurrentPage();
				if (currentPage.getName().equals(DatabaseWizardPage.class.getSimpleName())) {
					if (currentPage instanceof DatabaseWizardPage) {
						DatabaseWizardPage dataBasePage = (DatabaseWizardPage) currentPage;
						dataBasePage.updateDsModel();
					}
				}
			}
		});
		dialog.create();
		dialog.open();
		return null;
	}
}
